<?php
namespace jy_yxgoodserpfw;
defined('BY_JYA') or exit('error');
/*
	仓库员
*/
class store extends \plugin{
	function upkulog($post=array()){
		$codes=$post['codes'];
		$type=$post['type'];
		$code_type=1;
		if($post['type']==6){
			$type=0;
			$code_type=0;
		}elseif($post['type']==10){
			$type=1;
			$code_type=0;
		}elseif($post['type']==30){
			$type=3;
			$code_type=0;
		}
		foreach($codes as $cv){
			$cv['code']=trim($cv['code']);
			$log=array(
				'type'=>$type,
				'code'=>$cv['code'],
				'code_type'=>$code_type,
			);
			$this->model('model')->check_status($log);
		}
		foreach($codes as $cv){
			$cv['code']=trim($cv['code']);
			if(!$code_type){
				$item=pdo_get('jy_yxgoodserpfw_code',array('code'=>$cv['code'],'uniacid'=>$_SESSION['uniacid']));
				$code_num=1;
				$item['pre']=pdo_getcolumn('jy_yxgoodserpfw_batch',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['batch_id']),'pre');
			}else{
				$item=pdo_get('jy_yxgoodserpfw_xcode',array('code'=>$cv['code'],'uniacid'=>$_SESSION['uniacid']));
				$item['beinum']=$item['endnum']-$item['startnum']+1;
				$code_num=$item['beinum'];
				$item['pre']=pdo_getcolumn('jy_yxgoodserpfw_batch',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['batch_id']),'xpre');
			}
			if(!$item['id']){
				exi($code_type?$cv['code'].'标签不属于箱标签':$cv['code'].'标签不属于盒标签','error');
			}
			$isadd=pdo_get('jy_yxgoodserpfw_kulog',array('type'=>$type,'code'=>$cv['code'],'uniacid'=>$_SESSION['uniacid']));
			if($isadd['id']){
				//exi($cv['code'].'标签重复操作！','error');
			}
			$goods_id=$item['goods_id'];
			$batch_id=$item['batch_id'];
			$item['sn']=sprintf("%08d", $item['sn']);
			$item['presn']=$item['pre'].$item['sn'];
			$sn=$item['presn'];
			$xiang_id=$item['xiang_id'];
			$log=array(
				'type'=>$type,
				'code'=>$cv['code'],
				'merchid'=>$post['merchid'],
				'storeid'=>$post['storeid'],
				'workerid'=>$post['workerid'],
				'goods_id'=>$goods_id,
				'batch_id'=>$batch_id,
				'xiang_id'=>$xiang_id,
				'sn'=>$sn,
				'code_type'=>$code_type,
				'xiang_num'=>1,
				'code_num'=>$code_num,
				'remark'=>$post['remark'],
			);
			$log['uniacid']=$_SESSION['uniacid'];
			$log['createtime']=TIMESTAMP;
			$this->model('model')->upcodelog_status($log);
			pdo_insert('jy_yxgoodserpfw_kulog',$log);
			if($type==1){
				if($code_type){
					pdo_update('jy_yxgoodserpfw_xcode',array('merchid'=>$post['merchid']),array('code'=>$cv['code'],'uniacid'=>$_SESSION['uniacid']));
					if($item['id']){
						pdo_update('jy_yxgoodserpfw_code',array('merchid'=>$post['merchid']),array('xiang_id'=>$item['id'],'uniacid'=>$_SESSION['uniacid']));
					}
				}else{
					pdo_update('jy_yxgoodserpfw_code',array('merchid'=>$post['merchid']),array('code'=>$cv['code'],'uniacid'=>$_SESSION['uniacid']));
				}
			}
		}
		return '';
	}
	function uphelog($post=array(),$xcode2=''){
		$xcode2=trim($xcode2);
		$xcode=pdo_get('jy_yxgoodserpfw_xcode',array('code'=>$xcode2,'uniacid'=>$_SESSION['uniacid']));
		if(!$xcode['id']){
			exi($xcode2.'箱标签不存在！','error');
		}
		if($xcode['log_status']){
			exi('箱码'.$xcode['code'].'不属于【未入库】状态，不能操作！','error');
		}
		//pdo_update('jy_yxgoodserpfw_code',array('log_status'=>0,'xiang_id'=>0),array('xiang_id'=>$xcode['id'],'uniacid'=>$_SESSION['uniacid']));
		$codes=$post['codes'];
		$type=4;
		$code_type=0;
		foreach($codes as &$cv2){
			$cv2['code']=trim($cv2['code']);
			$log=array(
				'type'=>0,
				'code'=>$cv2['code'],
				'code_type'=>$code_type,
			);
			$cv2['codeitem']=$this->model('model')->check_status($log,'','',$xcode);
		}
		$batch=array();
		foreach($codes as $cv){
			$cv['code']=trim($cv['code']);
			$item=$cv['codeitem']?:pdo_get('jy_yxgoodserpfw_code',array('log_status'=>0,'xiang_id'=>0,'code'=>trim($cv['code']),'uniacid'=>$_SESSION['uniacid']));
			$code_num=1;
			$batch[$item['batch_id']]=$batch[$item['batch_id']]?:pdo_getcolumn('jy_yxgoodserpfw_batch',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['batch_id']),'pre');
			$item['pre']=$batch[$item['batch_id']];
			if(!$item['id']){
				exi($code_type?$cv['code'].'标签不属于箱标签':$cv['code'].'标签不属于盒标签!!','error');
			}
			//$isadd=pdo_get('jy_yxgoodserpfw_kulog',array('type'=>0,'code'=>$cv['code'],'uniacid'=>$_SESSION['uniacid']));
			if($isadd['id']){
				//exi($cv['code'].'标签重复操作！','error');
			}
			$goods_id=$item['goods_id'];
			$batch_id=$item['batch_id'];
			$item['sn']=sprintf("%08d", $item['sn']);
			$item['presn']=$item['pre'].$item['sn'];
			$sn=$item['presn'];
			$xiang_id=$xcode['id'];
			$log=array(
				'type'=>0,
				'code'=>$cv['code'],
				'storeid'=>$post['storeid'],
				'workerid'=>$post['workerid'],
				'goods_id'=>$goods_id,
				'batch_id'=>$batch_id,
				'xiang_id'=>$xiang_id,
				'sn'=>$sn,
				'code_type'=>$code_type,
				'xiang_num'=>1,
				'code_num'=>$code_num,
				'remark'=>$post['remark'],
			);
			$log['uniacid']=$_SESSION['uniacid'];
			$log['createtime']=TIMESTAMP;
			//$this->model('model')->upcodelog_status($log);
			pdo_insert('jy_yxgoodserpfw_kulog',$log);
			pdo_update('jy_yxgoodserpfw_code',array('log_status'=>1,'xiang_id'=>$xiang_id),array('log_status'=>0,'code'=>$cv['code'],'uniacid'=>$_SESSION['uniacid']));
		}
		$xcode['pre']=pdo_getcolumn('jy_yxgoodserpfw_batch',array('uniacid'=>$_SESSION['uniacid'],'id'=>$xcode['batch_id']),'xpre');
		$xcode['sn']=sprintf("%08d", $xcode['sn']);
		$xcode['presn']=$xcode['pre'].$xcode['sn'];
		$sn=$xcode['presn'];
		$log=array(
				'type'=>0,
				'code'=>$xcode['code'],
				'storeid'=>$post['storeid'],
				'workerid'=>$post['workerid'],
				'goods_id'=>$xcode['batch_id'],
				'batch_id'=>$xcode['batch_id'],
				'sn'=>$sn,
				'xiang_num'=>1,
				'code_type'=>1,
				'code_num'=>$xcode['beinum'],
				'remark'=>$post['remark'],
		);
		$log['uniacid']=$_SESSION['uniacid'];
		$log['createtime']=time();
		pdo_insert('jy_yxgoodserpfw_kulog',$log);
		$update=array('log_status'=>1);
		pdo_update('jy_yxgoodserpfw_xcode',$update,array('log_status'=>0,'code'=>$xcode['code'],'uniacid'=>$_SESSION['uniacid']));
		return '';
	}
	function uphelog2($post=array(),$xcode2=''){
		$xcode2=trim($xcode2);
		$xcode=pdo_get('jy_yxgoodserpfw_xcode',array('code'=>$xcode2,'uniacid'=>$_SESSION['uniacid']));
		if(!$xcode['id']){
			exi($xcode2.'箱标签不存在！','error');
		}
		if($xcode['log_status']){
			exi('箱码'.$xcode['code'].'不属于【未入库】状态，不能操作！','error');
		}
		pdo_update('jy_yxgoodserpfw_code',array('xcode2'=>''),array('xcode2'=>$xcode2,'xiang_id'=>$xcode['id'],'uniacid'=>$_SESSION['uniacid']));
		$codes=$post['codes'];
		$xcode['pre']=pdo_getcolumn('jy_yxgoodserpfw_batch',array('uniacid'=>$_SESSION['uniacid'],'id'=>$xcode['batch_id']),'xpre');
		$xcode['sn']=sprintf("%08d", $xcode['sn']);
		$xcode['presn']=$xcode['pre'].$xcode['sn'];
		$sn=$xcode['presn'];
		$log=array(
				'type'=>0,
				'code'=>$xcode['code'],
				'storeid'=>$post['storeid'],
				'workerid'=>$post['workerid'],
				'goods_id'=>$xcode['batch_id'],
				'batch_id'=>$xcode['batch_id'],
				'sn'=>$sn,
				'xiang_num'=>1,
				'code_type'=>1,
				'code_num'=>$xcode['beinum'],
				'remark'=>$post['remark'],
		);
		$log['uniacid']=$_SESSION['uniacid'];
		$log['createtime']=time();
		pdo_insert('jy_yxgoodserpfw_kulog',$log);
		$update=array('log_status'=>1);
		pdo_update('jy_yxgoodserpfw_xcode',$update,array('log_status'=>0,'code'=>$xcode['code'],'uniacid'=>$_SESSION['uniacid']));
		return '';
	}
	function update($goods_id,$data){
		$goods=array_fields(array('title','price','image','content','url'),$data);
		pdo_update('jy_yxgoodserpfw_goods',$goods,array('id'=>$goods_id,'uniacid'=>$_SESSION['uniacid']));
	}
}